package com.red.wine.ui;

import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.adapter.HisAdapter;
import com.red.wine.model.TasteBySql;
import com.red.wine.model.TasteInfo;
import com.red.wine.sql.HisSqlHelper;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.StringUtil;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
/**
 * 我的收藏
 */
public class FavoritesActivity extends BaseActivity{
	private ImageView back;
	private Context context;
	private HisSqlHelper hisSqlHelper;
	ListView listView;
	HisAdapter adapter;
	int position = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorites_layout);
		context = this;
		hisSqlHelper = new HisSqlHelper();
		WindowManager windowManager = getWindowManager();
		DisplayMetrics disMetrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(disMetrics);
		
		hisSqlHelper.open(WineApp.getInstance().getSqlHelper());
		back = (ImageView) findViewById(R.id.faco_info_back);
		listView = (ListView) findViewById(R.id.my_collection_listview);
		listView.setCacheColorHint(Color.TRANSPARENT);
		
		if(hisSqlHelper != null && hisSqlHelper.getCount() > 0){
			adapter = new HisAdapter(context, hisSqlHelper.getClientInfo());
		}else{
			Toast.makeText(context, "暂无收藏", Toast.LENGTH_SHORT).show();
		}
		back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				IntentUtil.redirectToNext(context, MyCenterActivity.class);
				finish();
			}
		});
		if(adapter != null){
			listView.setAdapter(adapter);
		}
		
		registerForContextMenu(listView);
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				position = arg2;
				return false;
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				TasteBySql info = (TasteBySql) adapter.getItem(arg2);
				Bundle bundle = new Bundle();
				bundle.putSerializable("info", null);
				bundle.putBoolean("fromsql", true);
				bundle.putSerializable("tasetinfo", info);
				bundle.putInt("index", arg2);
				IntentUtil.redirectToNext(context, TasteSecondActivity.class, bundle);
				hideLoading();
			}
		});
	}
	 
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, 0, 0, "删除");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem aItem) {
		ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo();
		TasteBySql info = (TasteBySql) adapter.getItem(position);
		hisSqlHelper.deleteAttribute(info.getId());
		adapter.refresh(hisSqlHelper.getClientInfo());

//		hisSqlHelper.deleteAttribute(position);
//		adapter.refresh(hisSqlHelper.getClientInfo());
//		Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
		return false;
	}
 
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			IntentUtil.redirectToNext(context, MyCenterActivity.class);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
